package com.thunder.soft.vm;

import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.thunder.soft.bean.UserBean;
import com.thunder.soft.common.base.BaseBean;
import com.thunder.soft.common.base.BaseViewModel;
import com.thunder.soft.common.net.HttpClient;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegisterViewModel extends BaseViewModel {
    private static String TAG = RegisterViewModel.class.getName();
    public MutableLiveData<UserBean> liveData = new MutableLiveData<>();
    public ObservableField<String> userName = new ObservableField<>();
    public ObservableField<String> passWord = new ObservableField<>();
    public ObservableField<String> rePassWord = new ObservableField<>();

    @Override
    public void onCreate(@NonNull LifecycleOwner owner) {
        super.onCreate(owner);
    }

    public void onRegisterClick(){

        if (TextUtils.isEmpty(userName.get())) {
            getErrorMsg().postValue("请输入账号");
            return;
        }
        if (TextUtils.isEmpty(passWord.get())) {
            getErrorMsg().postValue("请输入密码");
            return;
        }
        if (TextUtils.isEmpty(rePassWord.get())) {
            getErrorMsg().postValue("请确认密码");
            return;
        }else {
            if (!rePassWord.get().equals(passWord.get())){
                getErrorMsg().postValue("两次密码输入不一致");
                return;
            }
        }

        RequestBody requestBody = new FormBody.Builder()
                .add("username", Objects.requireNonNull(userName.get()))
                .add("password", Objects.requireNonNull(passWord.get()))
                .add("repassword", Objects.requireNonNull(rePassWord.get()))
                .build();

        HttpClient.getInstance().request("user/register", "POST", requestBody, new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                getErrorMsg().postValue(e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                Headers headers =response.headers();
                List<String> cookie = headers.values("Set-Cookie");
                HttpClient.getInstance().setCookie(cookie);
                Gson gson = new Gson();
                String responseStr = response.body().string();
                BaseBean<UserBean> userBean = gson.fromJson(responseStr,
                        new TypeToken<BaseBean<UserBean>>() {
                        }.getType());

                if (userBean.getErrorCode() == 0) {
                    HttpClient.getInstance().setPassword(passWord.get());
                    HttpClient.getInstance().setUserName(userName.get());
                    liveData.postValue(userBean.getData());
                }else {
                    getErrorMsg().postValue(userBean.getErrorMsg());
                }
            }
        });
    }

    public void onBackPressed(){
        getFinishActivity().postValue(1);
    }

    public MutableLiveData<UserBean> getLiveData() {
        return liveData;
    }
}
